package com.wey.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.wey.service.IRedBaoServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;

@Slf4j
@Service
public class RedBaoserverImpl implements IRedBaoServer  {

    public static Double money = 100d;
    public static Integer count = 5;
    public static final Double MIN = 0.01;
    @Override
    public String get(String userName) {
        log.info("当前抢红包的用户是：" + userName);
        double price;
        if (count == 0){
            return "红包已经抢光啦";
        }
        if (count == 1){
            price = money;
        } else {
            // 还有红包，计算最大金额
            double maybe = money - (count - 1)*MIN;
            log.info("当前能抽红包的最大金额是：" + maybe);
            price = RandomUtil.randomDouble(MIN, maybe, 2, RoundingMode.DOWN);
        }
        money = money - price;
        log.info("当前抢到的金额是：" + price + "元，还剩：" + money + "元");
        count--;
        return "抢到了" + price + "元";
    }

}
